package com.example.adminframework.controller;


import com.example.adminframework.common.annotation.LoginSign;
import com.example.adminframework.constant.LoginConstant;
import com.example.adminframework.jwt.JwtIgnore;
import com.example.adminframework.response.ResultContent;
import com.example.adminframework.util.CaptchaUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.Map;


@Api(tags = "登录模块")
@RestController
@PropertySource("classpath:application.yml")
public class UserLoginController {


    private static final Logger log= LoggerFactory.getLogger(UserLoginController.class);

//    @Value("${login.error.maxCount:5}")
//    private Integer maxLoginErrorCount;
//
//    @Value("${token.session.expire:300}")
//    private Integer tokenSessionMax;
//
//    @Value("${login.captchaEnabled}")
//    private Boolean captchaEnabled;



    /**
     * 获取验证码
     *
     * @param request  请求
     * @param response 响应
     */
    @LoginSign
    @JwtIgnore
    @ApiOperation("获取验证码")
    @RequestMapping(value = "/captcha", method = RequestMethod.GET)
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response) {
        // 调用工具类生成的验证码和验证码图片
        Map<String, Object> codeMap = CaptchaUtil.generateCodeAndPic();
        // 将四位数字的验证码保存到Session中。
        HttpSession session = request.getSession();

        System.out.println(codeMap.get("code").toString());
        session.setAttribute(LoginConstant.CAPTCHA, codeMap.get("code").toString());
        // 禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -1);
        response.setContentType("image/jpeg");
        // 将图像输出到Servlet输出流中。
        ServletOutputStream sos;
        try {
            sos = response.getOutputStream();
            ImageIO.write((RenderedImage) codeMap.get("codePic"), "jpeg", sos);
            sos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            log.error(e.getMessage(), e);
        }
    }

    @LoginSign
    @JwtIgnore
    @ApiOperation("验证验证码")
    @RequestMapping(value = "/check_captcha", method = RequestMethod.GET)
    public ResultContent CheckCaptcha(HttpServletRequest request, HttpServletResponse response, Integer code){
        HttpSession session = request.getSession();
        Integer attribute = Integer.valueOf(String.valueOf(session.getAttribute(LoginConstant.CAPTCHA)));
        if (code.equals(attribute)){
            return ResultContent.success("111");
        }else {
            return ResultContent.success("222");
        }
    }
}
